﻿using launcher.Models;
using launcher.Services;
using launcher.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace launcher.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class SettingPage : Page
    {
        private readonly LocalSettings m_localSettings = AppViewModel.Current.AppModel.LocalSettings;

        public SettingPage()
        {
            this.InitializeComponent();
            stopCheckBox.IsChecked = (m_localSettings.KillProcessWhenStop == true);
        }

        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.GoBack();
        }

        private void cachedDataAllCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            cachedPathCheckBox.IsChecked = true;
            cachedTextFilterCheckBox.IsChecked = true;
            cachedRegexFilterCheckBox.IsChecked = true;
        }

        private void cachedDataAllCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            cachedPathCheckBox.IsChecked = false;
            cachedTextFilterCheckBox.IsChecked = false;
            cachedRegexFilterCheckBox.IsChecked = false;
        }

        private void cachedDataAllCheckBox_Indeterminate(object sender, RoutedEventArgs e)
        {
            // If the SelectAll box is checked (all options are selected),
            // clicking the box will change it to its indeterminate state.
            // Instead, we want to uncheck all the boxes,
            // so we do this programatically. The indeterminate state should
            // only be set programatically, not by the user.

            if ((cachedPathCheckBox.IsChecked == true) 
                && (cachedTextFilterCheckBox.IsChecked == true) 
                && (cachedRegexFilterCheckBox.IsChecked == true))
            {
                // This will cause SelectAll_Unchecked to be executed, so
                // we don't need to uncheck the other boxes here.
                cachedDataAllCheckBox.IsChecked = false;
            }
        }

        private void SetCachedDataAllCheckBoxState()
        {
            // Controls are null the first time this is called, so we just
            // need to perform a null check on any one of the controls.
            if (cachedPathCheckBox != null)
            {
                if ((cachedPathCheckBox.IsChecked == true)
                    && (cachedTextFilterCheckBox.IsChecked == true)
                    && (cachedRegexFilterCheckBox.IsChecked == true))
                {
                    cachedDataAllCheckBox.IsChecked = true;
                }
                else if ((cachedPathCheckBox.IsChecked == false)
                    && (cachedTextFilterCheckBox.IsChecked == false) 
                    && (cachedRegexFilterCheckBox.IsChecked == false))
                {
                    cachedDataAllCheckBox.IsChecked = false;
                }
                else
                {
                    // Set third state (indeterminate) by setting IsChecked to null.
                    cachedDataAllCheckBox.IsChecked = null;
                }
            }
        }

        private void cachedDataItemCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            SetCachedDataAllCheckBoxState();
        }

        private void cachedDataItemCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            SetCachedDataAllCheckBoxState();
        }

        private void clearCacheButton_Click(object sender, RoutedEventArgs e)
        {
            if (cachedPathCheckBox.IsChecked == true)
            {
                m_localSettings.HistoryExecutableFiles = null;
                cachedPathCheckBox.IsEnabled = false;
            }
            if (cachedTextFilterCheckBox.IsChecked == true)
            {
                m_localSettings.HistroryTextFilters = null;
                cachedTextFilterCheckBox.IsEnabled = false;
            }
            if (cachedRegexFilterCheckBox.IsChecked == true)
            {
                m_localSettings.HistoryRegexFilters = null;
                cachedRegexFilterCheckBox.IsEnabled = false;
            }

            if (!cachedPathCheckBox.IsEnabled 
                && !cachedTextFilterCheckBox.IsEnabled 
                && !cachedRegexFilterCheckBox.IsEnabled)
            {
                cachedDataAllCheckBox.IsEnabled = false;
                clearCacheButton.IsEnabled = false;
            }
        }

        private void stopCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            m_localSettings.KillProcessWhenStop = true;
        }

        private void stopCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            m_localSettings.KillProcessWhenStop = null;
        }
    }
}
